package com.ljl.rest;

import com.alibaba.fastjson.JSONObject;
import com.ljl.entity.Account;
import com.ljl.exception.BadRequestException;
import com.ljl.service.AccountService;
import com.ljl.service.dto.AccountQueryCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author 刘家良
 * @date 2022/09/09 00:16
 * @description
 */
@RequestMapping("/api/account")
@RestController
@RequiredArgsConstructor
public class AccountController {

    private final AccountService accountService;

    @PostMapping("/signIn")
    public ResponseEntity<Object> signIn(@RequestBody JSONObject jsonObject) {
        Long accountId = jsonObject.getLong("accountId");
        if (accountId == null) {
            throw new BadRequestException("参数获取失败");
        }
        return new ResponseEntity<>(accountService.signInByAccountId(accountId), HttpStatus.OK);
    }

    @GetMapping
    public ResponseEntity<Object> queryAll(AccountQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(accountService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<Object> create(@RequestBody Account account) {
        return new ResponseEntity<>(accountService.create(account), HttpStatus.CREATED);
    }

    @PutMapping
    public ResponseEntity<Object> update(@RequestBody Account account) {
        return new ResponseEntity<>(accountService.update(account), HttpStatus.OK);
    }

    @DeleteMapping
    public ResponseEntity<Object> update(@RequestBody Set<Long> ids) {
        accountService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
